package cyou.breathe.blog.article.facade;

import cn.hutool.core.lang.Assert;
import cyou.breathe.blog.api.article.model.ArticleVO;
import cyou.breathe.blog.api.article.request.ArticleCreateRequest;
import cyou.breathe.blog.api.article.request.ArticleDeleteRequest;
import cyou.breathe.blog.api.article.request.ArticleQueryRequest;
import cyou.breathe.blog.api.article.request.ArticleUpdateRequest;
import cyou.breathe.blog.api.article.request.condition.ArticleFolderIdAndAuthorIdQueryCondition;
import cyou.breathe.blog.api.article.response.ArticleCreateResponse;
import cyou.breathe.blog.api.article.response.ArticleDeleteResponse;
import cyou.breathe.blog.api.article.response.ArticleQueryResponse;
import cyou.breathe.blog.api.article.response.ArticleUpdateResponse;
import cyou.breathe.blog.api.article.service.ArticleFacadeService;
import cyou.breathe.blog.api.folder.request.ArticleDeleteByFolderIdRequest;
import cyou.breathe.blog.api.folder.response.ArticleDeleteByFolderIdResponse;
import cyou.breathe.blog.article.domain.entity.Article;
import cyou.breathe.blog.article.domain.entity.convertor.ArticleConvertor;
import cyou.breathe.blog.article.domain.service.ArticleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author: breathe
 * @createTime: 2025-09-03
 */
@DubboService(version = "1.0.0")
public class ArticleFacadeServiceImpl implements ArticleFacadeService {

    @Autowired
    private ArticleService articleService;

    @Override
    public ArticleCreateResponse createArticle(ArticleCreateRequest articleCreateRequest) {
        return articleService.createArticle(articleCreateRequest);
    }

    @Override
    public ArticleUpdateResponse updateArticle(ArticleUpdateRequest articleUpdateRequest) {
        return articleService.updateArticle(articleUpdateRequest);
    }

    @Override
    public ArticleDeleteResponse deleteArticle(ArticleDeleteRequest articleDeleteRequest) {
        return articleService.deleteArticle(articleDeleteRequest);
    }

    /**
     * 获取文章： 现在只实现了根据文件夹ID和用户ID查询相应的文章
     * @param articleQueryRequest
     * @return
     */
    @Override
    public ArticleQueryResponse queryArticles(ArticleQueryRequest articleQueryRequest) {

        List<Article> articles = switch(articleQueryRequest.getArticleQueryCondition()) {
            case ArticleFolderIdAndAuthorIdQueryCondition articleFolderIdAndAuthorIdQueryCondition:
                yield articleService.queryArticlesByAuthorIdAndFolderId(articleFolderIdAndAuthorIdQueryCondition.getFolderId(), articleFolderIdAndAuthorIdQueryCondition.getAuthorId());
            default:
                throw new UnsupportedOperationException(articleQueryRequest.getArticleQueryCondition() + "'' is not supported");
        };
        List<ArticleVO> list = articles.stream()
                .map(ArticleConvertor.INSTANCE::mapToVO)
                .toList();
        ArticleQueryResponse articleQueryResponse = new ArticleQueryResponse();
        articleQueryResponse.setSuccess(true);
        articleQueryResponse.setData(list);

        return articleQueryResponse;
    }

    @Override
    public ArticleDeleteByFolderIdResponse deleteArticleByFolderId(ArticleDeleteByFolderIdRequest request) {
        ArticleDeleteByFolderIdResponse response = new ArticleDeleteByFolderIdResponse();

        List<Article> articles = articleService.queryArticlesByAuthorIdAndFolderId(request.getFolderId(), request.getAuthorId());

        articles.forEach(article -> {
            boolean result = articleService.removeById(article.getId());

            Assert.isTrue(result, "删除文章失败");
        });

        response.setSuccess(true);
        response.setFolderId(response.getFolderId());

        return response;
    }
}
